<%@page import="content.CategoryContent"%>
<%@page import="transfer.Commodity"%>
<%@page import="java.util.ArrayList"%>
<%@page import="content.CommodityContent"%>
<script type="text/javascript">
    $(document).ready(function() {

    });
</script>
<div class="box">
    <table class="table" rules=all>
        <tr>
            <td width="30px;"><input type="checkbox" id='all_commodities' onclick='select_all_commodities()'></td>
            <td>Title</td>
            <td>Price, $</td>
            <td width="104px">

                <div id="category-slider">
                    <span><a href="admin.jsp?content=product&type=0">All categories</a></span>
                    <%
                        ArrayList categories = CategoryContent.getCategories(10);
                        if (categories != null)
                            for (int i = 0; i < categories.size(); i++) {

                    %>
                    <span>
                        <a>
                            <%= ((transfer.CommodityCategory) categories.get(i)).getNameCategory()%>
                        </a>
                    </span>
                    <%
                        }
                    %>
                </div>

            </td>
        </tr>
        <%
            int category = 0;
            int pageCount = 1;
            try {
                String categoryParameter = request.getParameter("type");
                String pageParameter = request.getParameter("page");
                if (categoryParameter != null) {
                    category = Integer.parseInt(categoryParameter);
                }
                if (pageParameter != null) {
                    pageCount = Integer.parseInt(pageParameter);
                }
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }

            int count = 20;
            double price = 0;
            
            ArrayList commodityList;
            if (category != 0) 
                commodityList = CommodityContent.getGoodsByCategory(category, pageCount, count);
            else
                commodityList = CommodityContent.getAllGoods(pageCount, count);
            
            for (int i = 0; i < commodityList.size(); i++) {
                Commodity commodity = (Commodity) commodityList.get(i);
        %>
        <tr>
            <td>
            <%
                if (!commodity.getOutOfProduction().booleanValue()) {
            %>
                    <input type="checkbox" id='<%= commodity.getIdCommodity().intValue() %>' name='commodities' >
            <%
                }
            %>
            </td>
            <td><a href="admin.jsp?content=product&operation=edit&id=<%= commodity.getIdCommodity() %>" ><%=commodity.getNameCommodity()%></a></td>
            <td>
                <%
                    price = commodity.getPrice().doubleValue();
                    if ((Math.round(price) - price) == 0) {
                        out.print((int) price);
                    } else {
                        out.print(price);
                    }
                %> 
            </td>
            <td><%=commodity.getCategory().getNameCategory()%></td>
        </tr>
        <% }%>
    </table>
</div>

<%
    int goodsAmount;
    if (category != 0) 
        goodsAmount = CommodityContent.getAmountGoodsOfCategory(new Integer(category));
    else
        goodsAmount = CommodityContent.getAmountGoods();
    int pages = (int) (goodsAmount / count);
    if (goodsAmount % count != 0) {
        pages++;
    }
%>

<div id="pagination">
    <%
        if (pageCount > 1) {
    %>

    <a href="admin.jsp?content=product&type=<%= category%>&page=1"><<</a>

    <%
    } else {
    %>      
    <span id="selected-page"><<</span>
    <%        }
        if (pageCount > 1) {
    %>

    <a href="admin.jsp?content=product&type=<%= category%>&page=<%= pageCount - 1%>"><</a>

    <%
    } else {
    %>      

    <span id="selected-page"><</span>

    <%        }

        if (pageCount - 1 > 0) {
    %>

    <a href="admin.jsp?content=product&type=<%= category%>&page=<%= pageCount - 1%>"><%= pageCount - 1%></a>

    <%
        }
    %>

    <span id="selected-page"><%= pageCount%></span>

    <%
        if (pageCount + 1 <= pages) {
    %>

    <a href="admin.jsp?content=product&type=<%= category%>&page=<%= pageCount + 1%>"><%= pageCount + 1%></a>

    <%
        }
        if (pages != pageCount) {
    %>

    <a href="admin.jsp?content=product&type=<%= category%>&page=<%= pageCount + 1%>">></a>

    <%
    } else {
    %>
    <span id="selected-page">></span>
    <%        }
    %>


    <%
        if (pageCount != pages) {
    %>
    <a href="admin.jsp?content=product&type=<%= category%>&page=<%= pages%>">>></a>
    <%
    } else {
    %>      
    <span id="selected-page">>></span>
    <%        }
    %>

    <div id="results">Show <%= pageCount%> of <%= pages%> pages</div>
</div>

